﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StemServiceTest.StemService;
using System.IO;

namespace StemServiceTest
{
    /// <summary>
    /// Contains basic tests to validate functionality
    /// of database, cloud storage and wcf based webservice.
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            runComponentTests();
            
            // runServiceTests();
        }

        /// <summary>
        /// Run tests directly interacting with database and azure storage components.
        /// </summary>
        private static void runComponentTests()
        {
            string publisher = "css590";
            string term = "test";

            DatabaseHandler dbHandler = new DatabaseHandler();
            CloudHandler cloudHandler = new CloudHandler();

            // clear previous state
            dbHandler.ResetState();
            Console.WriteLine("ResetState: True");

            // publish metadata of a presentation
            uint idOfPresentation = dbHandler.PublishMetadata(publisher, term);
            Console.WriteLine("PublishMetadata: " + (idOfPresentation == 1));

            // upload image
            byte[] content;
            using (FileStream image = File.OpenRead(@"d:\lavipicture.jpg"))
            {
                content = new byte[image.Length];
                image.Read(content, 0, (int)image.Length);
            }

            Uri imageUri = cloudHandler.AddContent(idOfPresentation, ContentTypes.JPEG, content, 1);
            Console.WriteLine("UploadContent: " + imageUri.ToString() + " True");

            // prepare slides of presentation
            Slide[] contents = new Slide[3];
            contents[0] = new Slide();
            contents[0].Question = "what is it?";
            contents[0].Answer = "just a concept";
            contents[0].MediaType = MediaTypes.TextOnly;

            contents[1] = new Slide();
            contents[1].Question = "how it is made?";
            contents[1].Answer = "made from a,b,c";
            contents[1].MediaType = MediaTypes.TextWithImage;
            contents[1].ImageSource = imageUri;

            contents[2] = new Slide();
            contents[2].Question = "where it is made?";
            contents[2].Answer = string.Empty;
            contents[2].MediaType = MediaTypes.AudioWithImage;
            contents[2].ImageSource = imageUri;
            contents[2].MediaSource = imageUri;
            contents[2].MediaDuration = 30;

            // publish content into db
            dbHandler.PublishContent(idOfPresentation, contents);
            Console.WriteLine("PublishContent: True");

            // search for content in db, without publisher
            SearchResults results = dbHandler.Search(string.Empty, term);
            Console.WriteLine("Search (without publisher): " + (results.Presentations[0].Slides.Length == contents.Length));

            // search for content in db, with publisher
            results = dbHandler.Search(publisher, term);
            Console.WriteLine("Search (with publisher): " + (results.Presentations[0].Slides.Length == contents.Length));
        }

        /// <summary>
        /// Run tests indirectly interacting with database and azure storage
        /// via service, i.e. how clients are going to interact.
        /// </summary>
        private static void runServiceTests()
        {
            string publisher = "css5901";
            string term = "test3";

            DatabaseHandler dbHandler = new DatabaseHandler();
            dbHandler.ResetState();
            Console.WriteLine("ResetState: True");

            StemServiceClient client = new StemServiceClient();
            client.Open();
            uint presentationId = client.PublishMetadata(publisher, term);
            Console.WriteLine("PublishMetadata: " + (presentationId > uint.MinValue));

            // upload image
            byte[] content;
            using (FileStream image = File.OpenRead(@"d:\lavipicture.jpg"))
            {
                content = new byte[image.Length];
                image.Read(content, 0, (int)image.Length);
            }
            Uri imageUri = client.UploadContent(presentationId, StemService.ContentTypes.JPEG, content);
            Console.WriteLine("UploadContent: " + imageUri.ToString() + " True");

            // prepare slides of presentation
            Slide[] contents = new Slide[3];
            contents[0] = new Slide();
            contents[0].Question = "what is it?";
            contents[0].Answer = "just a concept";
            contents[0].MediaType = MediaTypes.TextOnly;

            contents[1] = new Slide();
            contents[1].Question = "how it is made?";
            contents[1].Answer = "made from a,b,c";
            contents[1].MediaType = MediaTypes.TextWithImage;
            contents[1].ImageSource = imageUri;

            contents[2] = new Slide();
            contents[2].Question = "where it is made?";
            contents[2].Answer = string.Empty;
            contents[2].MediaType = MediaTypes.AudioWithImage;
            contents[2].ImageSource = imageUri;
            contents[2].MediaSource = imageUri;
            contents[2].MediaDuration = 30;

            // publish content into db
            client.PublishContent(presentationId, contents);
            Console.WriteLine("PublishContent: True");

            // search for content in db, without publisher
            SearchResults results = client.Search(string.Empty, term);
            Console.WriteLine("Search (without publisher): " + (results.Presentations[0].Slides.Length == contents.Length));

            // search for content in db, with publisher
            results = client.Search(publisher, term);
            Console.WriteLine("Search (with publisher): " + (results.Presentations[0].Slides.Length == contents.Length));

            client.Close();
        }
    }
}
